﻿ --[[
	Widget name	  	CodeHelper
	Author     	 	Kurapica
--]]

-- CodeHelpDB
do
	IGAS:NewAddon("IGAS_Studio")
	
	local _CodeHelpDB = {}
	local _Base = {}
	
	local _FuncHelp = {
		["AddCodeHelp"] = function(self, name, intro, body)
			self[name] = {
				["Intro"] = intro,
				["Body"] = body,
			}
		end,
		["SetBaseHelpDB"] = function(self, base)
			_Base[self] = base
		end,
		["GetCodeHelp"] = function(self, name, part)
			return (self[name] and self[name][part]) or ""
		end,
		["GetBaseHelpDB"] = function(self)
			return _Base[self]
		end,
	}
	
	local _Meta = {
		__index = function(_t, _k)
			if _FuncHelp[_k] then
				return _FuncHelp[_k]
			end
			if _Base[_t] then
				return _Base[_t][_k]
			end
		end,
		__newindex = function(_t, _k, _v)
			if _FuncHelp[_k] then
				error("Can't add ".._k, 2)
			end
			rawset(_t, _k, _v)
		end,
	}
	
	function CreateHelpDB(Table, Sub)
		if not Table then
			return nil
		end
		_CodeHelpDB[Table] = _CodeHelpDB[Table] or setmetatable({}, _Meta)
		if Sub then
			_CodeHelpDB[Table][Sub] = _CodeHelpDB[Table][Sub] or setmetatable({}, _Meta)
			
			return _CodeHelpDB[Table][Sub]
		else
			return _CodeHelpDB[Table]
		end
	end
end